import KeyAble from "../interface/KeyAble";

export default class TSObjectPool<T extends KeyAble> {

    private pool:any [] = []


    private className:string;


    // private getCount:number = 0;

    // private putCount:number = 0;

    constructor(className:string,type: { new(): T ;},count:number = 0){
        this.className = className;
        for (let index = 0; index < count; index++) {
            this.pool.push(new type());
        }
    }

    getClassName(){
        return this.className;
    }


    // activator<T extends PoolAble>(type: { new(): T ;} ): T {
    //     return new type();
    // }


    get<T extends KeyAble>(type: { new(): T ;} ): T {
        // let used = this.getCount - this.putCount;
        // if(used  > 100 ){
        //     cc.warn(' TSObjectPool is to lot ',this.className,' getCount ',this.getCount,' putCount ',this.putCount)
        // }
        // this.getCount ++;

        // cc.log('get class name ',this.className,' count ',this.pool.length)
        let go = this.pool.length > 0 ? this.pool.shift() : null;
        if(!go){
            go = new type();
        }
        // cc.log('get this.pool.length  ',this.pool.length,go.getKey() )
        // this.list.put(go.getKey(),go);
        return go;
    }

    put(instance:T){
        // this.putCount ++;
        this.pool.push(instance);
        // cc.log('put class name ',this.className,' count ',this.pool.length)
        // cc.log('put this.pool.length  ',this.pool.length,instance.getKey() )
        // this.remove(instance)
    }

    // putAll(){
    //     let list = this.list.array();
    //     while(list.length > 0){
    //         this.pool.push(list.shift())
    //     }
    // }

    clear(){
        this.pool = [];
        // this.getCount = 0;
        // this.putCount = 0;
    }
    // private remove(go:T){
    //     for (let index = 0; index < this.list.length; index++) {
    //         const element = this.list[index];
    //         if(element == go){
    //             this.list.splice(index,1)
    //             break;
    //         }
    //     }
    // }
 
}
